//
//  ZYCCommonSelectObjectPickerView.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/15.
//

#import "ZYCCommonSelectObjectPickerView.h"
#import "ZYCCommonBaseModel.h"

#define kDateViewH 258
#define kPickViewH 218

@interface ZYCCommonSelectObjectPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIView *dimView;
@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *comfirmButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, assign) NSInteger index;

@end
@implementation ZYCCommonSelectObjectPickerView

- (void)setTipString:(NSString *)tipString{
    _tipString = tipString;
    _titleLabel.text = tipString;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.00f];
        _index = 0;
        [self setupUI];
        [self setupConstraint];
    }
    return self;
}

- (void)setupUI{
    [self addSubview:self.dimView];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.cancelButton];
    [self.baseView addSubview:self.comfirmButton];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.line];
    [self.baseView addSubview:self.pickView];
}


- (void)setupConstraint{
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView.mas_left);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
        make.top.mas_equalTo(self.baseView);
    }];
    [self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView.mas_right);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
        make.top.mas_equalTo(self.baseView);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.baseView);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.baseView).offset(45.0f);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.baseView);
//        make.centerY.equalTo(self.baseView.mas_top).offset(22.5f);
        make.centerY.equalTo(self.cancelButton);
    }];
}

- (UIView *)dimView{
    if (!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.alpha = 0.3;
        _dimView.backgroundColor = [UIColor blackColor];
    }
    return _dimView;
}

- (UIView *)baseView{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kDateViewH)];
        _baseView.alpha = 1.0f;
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}


- (UIPickerView *)pickView {
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, kPickViewH)];
        _pickView.dataSource = self;
        _pickView.delegate = self;
    }
    return _pickView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel =  [UILabel LabelWithFont:kSystemFont(16.0f) titleColor:HEX_RGB(@"333333") textAlignment:ZZTextAlignmentCenter];
    }
    return _titleLabel;
}


- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EAEAEA");
    }
    return _line;
}
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] init];
        _cancelButton.tag = 1000;
        [_cancelButton setTitleColor:HEX_RGB(@"#A3A3A3") forState:UIControlStateNormal];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = kCustomFont(@"PingFangSC-Medium", 15.0f);
        [_cancelButton addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UIButton *)comfirmButton {
    if (!_comfirmButton) {
        _comfirmButton = [[UIButton alloc] init];
        _comfirmButton.tag = 1001;
        [_comfirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_comfirmButton setTitleColor:HEX_RGB(@"#2384D8") forState:UIControlStateNormal];
        _comfirmButton.titleLabel.font = kCustomFont(@"PingFangSC-Medium", 15.0f);
        [_comfirmButton addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comfirmButton;
}


- (void)clickBtnAction:(UIButton *)sender{
    [self dismiss];
    [[UIApplication sharedApplication] resignFirstResponder];
    if (sender.tag == 1001) {
        if ([self.delegate respondsToSelector:@selector(selectObjectPickerView:DidClickModel:)]) {
            [self.delegate selectObjectPickerView:self DidClickModel:self.dataSource[_index]];
        }
    }
}


- (void)dismiss{
    [UIView animateWithDuration:0.3f animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 258);
    }completion:^(BOOL finished) {
        self.hidden = YES;
        self.dimView.hidden = YES;
        self.baseView.hidden = YES;
    }];
}

- (void)show{
    self.hidden = NO;
    self.dimView.hidden = NO;
    self.baseView.hidden = NO;
    [UIView animateWithDuration:0.3f animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight-258, kScreenWidth, 258);
    }completion:^(BOOL finished) {
        
    }];
}

- (void)reload{
    [self.pickView reloadAllComponents];
    [self dismiss];
}

#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataSource.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    ZYCCommonBaseModel *model = self.dataSource[row];
    return model.name;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if ((self.dataSource.count-1)>=row) {
        _index = row;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismiss];
}

@end
